package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.project.entity.Issue;
import com.example.project.entity.Project;

/**
 * 项目Mapper接口
 * 
 * @author ruoyi
 * @date 2025-09-15
 */
@Mapper
public interface ProjectMapper{

	/**
	 * 查询项目
	 * 
	 * @param id 项目主键
	 * @return 项目
	 */
	@Select("select id, name, customer_name, description, state, homepage, identifier, color, customer_detail, operation_type, git_url, "
			+ "server_detail, domain, admin_domain, html_domain, sign_date, expir_date, server_expir_date, sign_detail, remark, develop_language, project_leader from t_project where del_flag = 0 and id = #{id}")
	public Project selectProjectById(Long id);

	/**
	 * 查询项目列表
	 * 
	 * @param projects 项目
	 * @return 项目集合
	 */
	@Select("<script>"
			+ "select id, name, customer_name, description, state, homepage, identifier, color, customer_detail, operation_type, git_url, "
			+ "server_detail, domain, admin_domain, html_domain, sign_date, expir_date, server_expir_date, sign_detail, remark, develop_language, project_leader from t_project where del_flag = 0"
			+ "<if test='project.name != null and project.name != \"\"'> and name like concat('%', #{project.name}, '%')</if>"
			+ "<if test='project.customerName != null and project.customerName != \"\"'> and customer_name like concat('%', #{project.customerName}, '%')</if>"
			+ "<if test='project.description != null and project.description != \"\"'> and description = #{project.description}</if>"
			+ "<if test='project.state != null'> and state = #{project.state}</if>"
			+ "<if test='project.homepage != null and project.homepage != \"\"'> and homepage = #{project.homepage}</if>"
			+ "<if test='project.identifier != null and project.identifier != \"\"'> and identifier = #{project.identifier}</if>"
			+ "<if test='project.color != null and project.color != \"\"'> and color = #{project.color}</if>"
			+ "<if test='project.customerDetail != null and project.customerDetail != \"\"'> and customer_detail = #{project.customerDetail}</if>"
			+ "<if test='project.operationType != null'> and operation_type = #{project.operationType}</if>"
			+ "<if test='project.gitUrl != null and project.gitUrl != \"\"'> and git_url = #{project.gitUrl}</if>"
			+ "<if test='project.serverDetail != null and project.serverDetail != \"\"'> and server_detail = #{project.serverDetail}</if>"
			+ "<if test='project.domain != null and project.domain != \"\"'> and domain = #{project.domain}</if>"
			+ "<if test='project.adminDomain != null and project.adminDomain != \"\"'> and admin_domain = #{project.adminDomain}</if>"
			+ "<if test='project.htmlDomain != null and project.htmlDomain != \"\"'> and html_domain = #{project.htmlDomain}</if>"
			+ "<if test='project.signDate != null'> and sign_date = #{project.signDate}</if>"
			+ "<if test='project.expirDate != null'> and expir_date = #{project.expirDate}</if>"
			+ "<if test='project.serverExpirDate != null'> and server_expir_date = #{project.serverExpirDate}</if>"
			+ "<if test='project.signDetail != null and project.signDetail != \"\"'> and sign_detail = #{project.signDetail}</if>"
			+ "<if test='project.developLanguage != null'> and develop_language = #{project.developLanguage}</if>"
			+ "${project.params.dataScope}" + "</script>")
	public List<Project> selectProjectList(Page<Issue> page,@Param("project")  Project project);

	/**
	 * 新增项目
	 * 
	 * @param projects 项目
	 * @return 结果
	 */
	@Insert("<script>" + "insert into t_project" + "<trim prefix='(' suffix=')' suffixOverrides=','>"
            + "<if test='name != null and name != \"\"'>name,</if>"
            + "<if test='customerName != null'>customer_name,</if>"
            + "<if test='description != null and description != \"\"'>description,</if>"
            + "<if test='state != null'>state,</if>"
            + "<if test='homepage != null'>homepage,</if>"
            + "<if test='identifier != null and identifier != \"\"'>identifier,</if>"
            + "<if test='color != null'>color,</if>"
            + "<if test='customerDetail != null'>customer_detail,</if>"
            + "<if test='operationType != null'>operation_type,</if>"
            + "<if test='gitUrl != null'>git_url,</if>"
            + "<if test='serverDetail != null'>server_detail,</if>"
            + "<if test='domain != null'>domain,</if>"
            + "<if test='adminDomain != null'>admin_domain,</if>"
            + "<if test='htmlDomain != null'>html_domain,</if>"
            + "<if test='signDate != null'>sign_date,</if>"
            + "<if test='expirDate != null'>expir_date,</if>"
            + "<if test='serverExpirDate != null'>server_expir_date,</if>"
            + "<if test='signDetail != null'>sign_detail,</if>"
            + "<if test='remark != null'>remark,</if>"
            + "<if test='developLanguage != null'>develop_language,</if>"
            + "<if test='projectLeader != null and projectLeader != 0'>project_leader,</if>"
            + "del_flag,"
            + "<if test='createBy != null'>create_by,</if>"
            + "<if test='createTime != null'>create_time,</if>"
            + "</trim>"
			+ "<trim prefix='values (' suffix=')' suffixOverrides=','>"
            + "<if test='name != null and name != \"\"'>#{name},</if>"
            + "<if test='customerName != null'>#{customerName},</if>"
            + "<if test='description != null and description != \"\"'>#{description},</if>"
            + "<if test='state != null'>#{state},</if>"
            + "<if test='homepage != null'>#{homepage},</if>"
            + "<if test='identifier != null and identifier != \"\"'>#{identifier},</if>"
            + "<if test='color != null'>#{color},</if>"
            + "<if test='customerDetail != null'>#{customerDetail},</if>"
            + "<if test='operationType != null'>#{operationType},</if>"
            + "<if test='gitUrl != null'>#{gitUrl},</if>"
            + "<if test='serverDetail != null'>#{serverDetail},</if>"
            + "<if test='domain != null'>#{domain},</if>"
            + "<if test='adminDomain != null'>#{adminDomain},</if>"
            + "<if test='htmlDomain != null'>#{htmlDomain},</if>"
            + "<if test='signDate != null'>#{signDate},</if>"
            + "<if test='expirDate != null'>#{expirDate},</if>"
            + "<if test='serverExpirDate != null'>#{serverExpirDate},</if>"
            + "<if test='signDetail != null'>#{signDetail},</if>"
            + "<if test='remark != null'>#{remark},</if>"
            + "<if test='developLanguage != null'>#{developLanguage},</if>"
            + "<if test='projectLeader != null and projectLeader != 0'>#{projectLeader},</if>"
            + "0,"
            + "<if test='createBy != null'>#{createBy},</if>"
            + "<if test='createTime != null'>#{createTime},</if>"
			+ "</trim>" + "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	public int insertProject(Project projects);

	/**
	 * 修改项目
	 * 
	 * @param projects 项目
	 * @return 结果
	 */
	@Update("<script>" + "update t_project" + "<trim prefix='SET' suffixOverrides=','>"
			+ "<if test='name != null and name != \"\"'>name = #{name},</if>"
			+ "<if test='customerName != null'>customer_name = #{customerName},</if>"
			+ "<if test='description != null and description != \"\"'>description = #{description},</if>"
			+ "<if test='state != null'>state = #{state},</if>"
			+ "<if test='homepage != null'>homepage = #{homepage},</if>"
			+ "<if test='color != null'>color = #{color},</if>"
			+ "<if test='customerDetail != null'>customer_detail = #{customerDetail},</if>"
			+ "<if test='operationType != null'>operation_type = #{operationType},</if>"
			+ "<if test='gitUrl != null'>git_url = #{gitUrl},</if>"
			+ "<if test='serverDetail != null'>server_detail = #{serverDetail},</if>"
			+ "<if test='domain != null'>domain = #{domain},</if>"
			+ "<if test='adminDomain != null'>admin_domain = #{adminDomain},</if>"
			+ "<if test='htmlDomain != null'>html_domain = #{htmlDomain},</if>"
			+ "<if test='signDate != null'>sign_date = #{signDate},</if>"
			+ "<if test='expirDate != null'>expir_date = #{expirDate},</if>"
			+ "<if test='serverExpirDate != null'>server_expir_date = #{serverExpirDate},</if>"
			+ "<if test='signDetail != null'>sign_detail = #{signDetail},</if>"
			+ "<if test='remark != null'>remark = #{remark},</if>"
			+ "<if test='developLanguage != null'>develop_language = #{developLanguage},</if>"
			+ "<if test='projectLeader != null'>project_leader = #{projectLeader},</if>"
			+ "<if test='updateBy != null'>update_by = #{updateBy},</if>"
			+ "<if test='updateTime != null'>update_time = #{updateTime},</if>" + "</trim>" 
			+ "where del_flag = 0 and id = #{id}"
			+ "</script>")
	public int updateProject(Project projects);

	/**
	 * 删除项目
	 * 
	 * @param id 项目主键
	 * @return 结果
	 */
//	@Delete("delete from t_project where id = #{id}")
	@Update("update t_project set del_flag = 1 where id = #{id}")
	public int deleteProjectById(Long id);

	/**
	 * 批量删除项目
	 * 
	 * @param ids 需要删除的数据主键集合
	 * @return 结果
	 */
	@Update("<script>" + "update t_project set del_flag = 1 where id in "
			+ "<foreach item='id' collection='array' open='(' separator=',' close=')'>" + "#{id}" + "</foreach>"
			+ "</script>")
	public int deleteProjectByIds(String[] ids);

	@Select("<script>" + "select count(1) from t_project where del_flag = 0 and identifier = #{identifier}"
			+ "<if test='id != 0'> and id = #{id}</if>" + "</script>")
	public int checkIdentifier(String identifier, Long id);

	@Select("<script>"
			+ "select id, name, customer_name, description, state, homepage, identifier, color, customer_detail, operation_type, git_url, "
			+ "server_detail, domain, admin_domain, html_domain, sign_date, expir_date, server_expir_date, sign_detail, remark, develop_language, project_leader from t_project where del_flag = 0"
			+ "<if test='_parameter >= 0'> and state = #{state}</if>"
			+ " order by state desc, create_time desc"
			+ "</script>")
	public List<Project> selectProjectAll(int state);

	@Update("update t_project set state = #{state}, update_time = sysdate() where id = #{id}")
	public int updateStatus(Long id, Integer state);

	/**
	 * 根据状态统计项目数量
	 * 
	 * @param state 项目状态
	 * @return 项目数量
	 */
	@Select("select count(*) from t_project where del_flag = 0 and state = #{state}")
	Integer selectCountByState(Integer state);
	
	/**
	 * 获取项目总数
	 * 
	 * @return 项目总数
	 */
	@Select("select count(*) from t_project where del_flag = 0")
	Long getCount();
}
